package com.api.oa.services.impl;

import cn.hutool.core.date.DateUtil;
import com.api.oa.mapper.OaAttendMapper;
import com.api.oa.model.entity.OaAttend;
import com.api.oa.model.vo.OaAttendStatics;
import com.api.oa.model.vo.OaAttendVO;
import com.api.oa.services.IOaAttendService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;


@Service
public class OaAttendServiceImpl implements IOaAttendService {


    @Value("${upTime}")
    private String upTime = "8:00";

    @Value("${downTime}")
    private String downTime = "17:00";

    @Autowired
    private OaAttendMapper oaAttendMapper;

    @Override
    public Integer add(OaAttend oaOrg) {
        OaAttend oneByUserCodeAndToday = oaAttendMapper.findOneByUserCodeAndToday(oaOrg.getAttendsUserId(), DateUtil.format(new Date(), "YYYY-MM-dd"));
        if (oneByUserCodeAndToday == null) {
            oaOrg.setAttendsType(0); //上班
        } else {
            oaOrg.setAttendsType(oneByUserCodeAndToday.getAttendsType() == 0 ? 1 : 0);
        }
        if (oaOrg.getAttendsType() == 0) {  //上班
            String[] split = upTime.split(":");
            LocalDateTime localDateTime = LocalDateTime.of(DateUtil.thisYear(), DateUtil.thisMonth() + 1, DateUtil.thisDayOfMonth(), Integer.parseInt(split[0]), Integer.parseInt(split[1]));
            oaOrg.setStatusId(localDateTime.isAfter(LocalDateTime.now()) ? 0 : 1);
        } else {
            String[] split = downTime.split(":");
            LocalDateTime localDateTime = LocalDateTime.of(DateUtil.thisYear(), DateUtil.thisMonth() + 1, DateUtil.thisDayOfMonth(), Integer.parseInt(split[0]), Integer.parseInt(split[1]));
            oaOrg.setStatusId(localDateTime.isAfter(LocalDateTime.now()) ? 2 : 0);
        }

        return oaAttendMapper.insert(oaOrg);
    }

    @Override
    public void remove(Long id) {
        oaAttendMapper.delete(id);
    }

    @Override
    public Integer update(OaAttend org) {
        return oaAttendMapper.update(org);
    }

    @Override
    public PageInfo<OaAttendVO> list(Integer pageIndex, Integer pageSize, String userName) {
        PageHelper.startPage(pageIndex, pageSize);
        PageInfo<OaAttendVO> pageInfo = new PageInfo<OaAttendVO>(oaAttendMapper.list(userName));
        return pageInfo;
    }


    @Override
    public OaAttend one(Long id) {
        return oaAttendMapper.findOne(id);
    }

    @Override
    public PageInfo<OaAttendStatics> listByWeek(String start, String end, String uid, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageInfo<OaAttendStatics> pageInfo = new PageInfo<OaAttendStatics>(oaAttendMapper.listByWeek(uid,start,end));
        return pageInfo;
    }

    @Override
    public PageInfo<OaAttendVO> listByUserToday(String uid, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageHelper.orderBy("attendsTime desc");
        PageInfo<OaAttendVO> pageInfo = new PageInfo<OaAttendVO>(oaAttendMapper.listByUserToday(uid, DateUtil.format(new Date(), "YYYY-MM-dd")));
        return pageInfo;
    }

    @Override
    public PageInfo<OaAttendStatics> listByToday(String uid,String govCode, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageInfo<OaAttendStatics> pageInfo = new PageInfo<OaAttendStatics>(oaAttendMapper.listByToday(uid,govCode));
        return pageInfo;
    }


}
